package cn.kgc.itrip.controller;

import cn.kgc.itrip.common.DtoUtil;
import cn.kgc.itrip.dto.Dto;
import cn.kgc.itrip.service.commentService.CommentService;
import cn.kgc.itrip.vo.comment.ItripAddCommentVO;
import cn.kgc.itrip.vo.comment.ItripSearchCommentVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @program: love_travel
 * @description: 评论和酒店星级
 * @author: Mr.Wang
 * @create: 2020-04-05 15:06
 **/

@RestController
@RequestMapping("/api/comment")
public class SystemCommentController {

    @Resource
    private CommentService commentService;

    /**
     * 查询出游类型列表
     * @return
     */
    @GetMapping(value = "/gettraveltype",produces = "application/json")
    public Dto getTravelType(){
        Dto dto=null;
        dto=commentService.findTravelType();
        return dto;
    }

    /**
     * 根据targetId查询评论照片(type=2)
     * @param targetId
     * @return
     */
    @GetMapping(value = "/getimg/{targetId}",produces = "application/json")
    public Dto getImg(@PathVariable String targetId){
        Dto dto=null;
        dto=commentService.findImg(targetId);
        return dto;
    }

    /**
     * 新增评论接口
     * @param request
     * @param itripAddCommentVO
     * @return
     */
    @PostMapping(value = "/add",produces = "application/json")
    public Dto addAppraise(HttpServletRequest request, @RequestBody ItripAddCommentVO itripAddCommentVO){
        Dto dto=null;
        String token = request.getHeader("token");
        String userAgent = request.getHeader("user-agent");
        try {
            dto=commentService.addAppraise(itripAddCommentVO,token,userAgent);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("新增评论失败","100003");
        }
        return dto;
    }

    /**
     * 根据评论类型查询评论列表，并分页显示
     * @param itripSearchCommentVO
     * @return
     */
    @PostMapping(value = "/getcommentlist",produces = "application/json")
    public Dto getcommentlist(@RequestBody ItripSearchCommentVO itripSearchCommentVO){
        Dto dto=null;
        try {
            dto=commentService.findCommentList(itripSearchCommentVO);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取评论列表错误","100020");
        }
        return dto;
    }

    /**
     * 获取酒店相关信息（酒店名称、酒店星级）
     * @param hotelId
     * @return
     */
    @GetMapping(value = "/gethoteldesc/{hotelId}",produces = "application/json")
    public Dto getHotelDesc(@PathVariable String hotelId){
        Dto dto=null;
        try {
            dto=commentService.findHotelDesc(hotelId);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取酒店相关信息错误","100021");
        }
        return dto;
    }

    /**
     * 据酒店id查询酒店平均分
     * @param hotelId
     * @return
     */
    @GetMapping(value = "/gethotelscore/{hotelId}",produces = "application/json")
    public Dto getHotelScore(@PathVariable String hotelId){
        Dto dto=null;
        try {
            dto=commentService.findHotelScore(hotelId);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取评分失败","100001");
        }
        return dto;
    }


    /**
     * 根据酒店id查询各类评论数量
     * @param hotelId
     * @return
     */
    @GetMapping(value = "/getcount/{hotelId}",produces = "application/json")
    public Dto getCount(@PathVariable String hotelId){
        Dto dto=null;
        try {
            dto=commentService.findCount(hotelId);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取酒店总评论数失败","100014");
        }
        return dto;
    }
}
